import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/myhome',  // 重定向:重新指向其它path,会改变网址
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import( '../views/Login.vue')
  },
  {
    path: '/index',
    name: 'Index',
    component: () => import( '../views/home/Index.vue'),
    children:[
      {
        path: '/myhome',
        name: 'MyHome',
        component: () => import( '../views/home/MyHome.vue')
      },
      {
        path: '/product',
        name: 'Product',
        component: () => import( '../views/home/Product.vue')
      },
      {
        path: '/order',
        name: 'Order',
        component: () => import( '../views/home/Order.vue')
      },
      {
        path: '/cart',
        name: 'Cart',
        component: () => import( '../views/home/Cart.vue')
      },
      {
        path: '/user',
        name: 'User',
        component: () => import( '../views/home/User.vue')
      },
      {
        path: '/detail',
        name: 'Detail',
        component: () => import( '../views/home/children/Detail.vue')
      },
      {
        path: '/save',
        name: 'Save',
        component: () => import( '../views/home/children/Save.vue')
      },
      {
        path: '/cartadd',
        name: 'Cartadd',
        component: () => import( '../views/home/children/Cartadd.vue')   
      },
      {
        path: '/orderdetail',
        name: 'OrderDetail',
        component: () => import( '../views/home/children/OrderDetail.vue')
      },
    ]
  }
]

const router = new VueRouter({
  mode:"history",
  routes
})

// 访问的白名单, 可以直接通行

router.beforeEach((to, from, next) => {
  // 访问的路径在白名单
  if(!sessionStorage.getItem('token') && to.path != "/login"){
    next('/login')
  }else if(sessionStorage.getItem('token') && to.path == "/login"){
    next('/index')
  }else{
    next()
  }
  
})
export default router
